﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using Semantics;
using System.Windows.Forms;
namespace GraphBase.Presentation.Extension
{
    /// <summary>
    /// 定位节点，保存上次节点的位置
    /// </summary>
    internal class VisualGraph
    {
        IGraph GraphDatabase;
        //glite.SqliteBase SqliteBase;
        public VisualGraph(IGraph gdb)//, glite.SqliteBase sqlitebase)
        {
            this.GraphDatabase = gdb;
            //this.SqliteBase = sqlitebase;
        }


        SortedList<int, NodePos> NodePositions = new SortedList<int, NodePos>();

        public void Reload()
        {
            IList<Node> allnodes = this.GraphDatabase.GetAllNodes();
            this.GraphDatabase.GetNodesAttributes(allnodes, Node.AttributesNames.Location);
            this.NodePositions.Clear();
            foreach (Node node in allnodes)
            {
                try
                {
                    object attribute;
                    if ( node.NodeAttributes.TryGetValue(Node.AttributesNames.Location, out attribute))
                    {
                        NodePos pos = new NodePos(node);
                        pos.cPoint = (System.Drawing.Point)attribute;
                        this.NodePositions.Add(pos.pNode.Id, pos);
                    }
                }
                catch (Exception exp)
                {
                    throw exp;
                }
            }
        }
        public NodePos GetNodePos(Node node)
        {
            NodePos pos = null;
            this.NodePositions.TryGetValue(node.Id, out pos);
            return pos;
        }
    }
}
